//
//  ActivityIndicatorView.m
//  ImGuider
//
//  Created by llt on 2017/6/27.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "ActivityIndicatorView.h"
#import "ActivityIndicatorBallPulseAnimation.h"

#define kActivityTag 2468

static const CGFloat kDGActivityIndicatorDefaultSize = 40.0f;

@interface ActivityIndicatorView () {
    CALayer *_animationLayer;
}

@property (nonatomic, strong) UIColor *tintColor;
@property (nonatomic) CGFloat size;

@property (nonatomic, readonly) BOOL animating;

@end

@implementation ActivityIndicatorView


+ (void)showHUDAddedTo:(UIView *)view {
    [self hideHUDAddedTo:view];
    
    [self showHUDAddedTo:view tintColor:kThemeColor];
}

+ (void)showHUDAddedTo:(UIView *)view tintColor:(UIColor *)tintColor {
    
    [self hideHUDAddedTo:view];
    ActivityIndicatorView *activityView = [[ActivityIndicatorView alloc] initWithTintColor:tintColor size:44];
    activityView.tag = kActivityTag;
    
    [view addSubview:activityView];
    
    [activityView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.equalTo(view);
    }];
    
    [activityView startAnimating];
    
    view.userInteractionEnabled = NO;
    
}


+ (void)showHUDAddedTo:(UIView *)view backgroundColor:(UIColor *)bgColor {
    
    [self hideHUDAddedTo:view];
    ActivityIndicatorView *activityView = [[ActivityIndicatorView alloc] initWithTintColor:kThemeColor size:44];
    activityView.tag = kActivityTag;
    activityView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    activityView.backgroundColor = bgColor;
    
    [view addSubview:activityView];
    
//    [activityView mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.centerX.equalTo(view.mas_centerX);
//        make.centerY.equalTo(view.mas_centerY);
//    }];
    
    [activityView startAnimating];
    
    view.userInteractionEnabled = NO;
    
}

+ (void)hideHUDAddedTo:(UIView *)view {
    
    UIView *activityView = [view viewWithTag:kActivityTag];
    
    
    if (activityView) {
        [activityView removeFromSuperview];
        [self hideHUDAddedTo:view];
    } else {
        
        view.userInteractionEnabled = YES;
    }
}

#pragma mark -
#pragma mark Constructors

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        _tintColor = [UIColor whiteColor];
        _size = kDGActivityIndicatorDefaultSize;
        [self commonInit];
    }
    return self;
}

- (id)init{
    return [self initWithTintColor:[UIColor whiteColor] size:kDGActivityIndicatorDefaultSize];
}

- (id)initWithTintColor:(UIColor *)tintColor {
    return [self initWithTintColor:tintColor size:kDGActivityIndicatorDefaultSize];
}

- (id)initWithTintColor:(UIColor *)tintColor size:(CGFloat)size {
    self = [super init];
    if (self) {
        _size = size;
        _tintColor = tintColor;
        [self commonInit];
    }
    return self;
}

#pragma mark -
#pragma mark Methods

- (void)commonInit {
    self.userInteractionEnabled = NO;
    self.hidden = YES;
    
    _animationLayer = [[CALayer alloc] init];
    [self.layer addSublayer:_animationLayer];
    
    [self setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
}

- (void)setupAnimation {
    
    _animationLayer.sublayers = nil;
    ActivityIndicatorBallPulseAnimation *animation = [[ActivityIndicatorBallPulseAnimation alloc] init];
    
    [animation setupAnimationInLayer:_animationLayer withSize:CGSizeMake(_size, _size) tintColor:_tintColor];
    _animationLayer.speed = 0.0f;
}

- (void)startAnimating {
    
    if (!_animationLayer.sublayers) {
        [self setupAnimation];
    }
    self.hidden = NO;
    _animationLayer.speed = 1.0f;
    _animating = YES;
}

- (void)stopAnimating {
    _animationLayer.speed = 0.0f;
    _animating = NO;
    self.hidden = YES;
}

#pragma mark -
#pragma mark Setters

- (void)setSize:(CGFloat)size {
    if (_size != size) {
        _size = size;
        
        [self setupAnimation];
        [self invalidateIntrinsicContentSize];
    }
}

- (void)setTintColor:(UIColor *)tintColor {
    if (![_tintColor isEqual:tintColor]) {
        _tintColor = tintColor;
        
        CGColorRef tintColorRef = tintColor.CGColor;
        for (CALayer *sublayer in _animationLayer.sublayers) {
            sublayer.backgroundColor = tintColorRef;
            
            if ([sublayer isKindOfClass:[CAShapeLayer class]]) {
                CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
                shapeLayer.strokeColor = tintColorRef;
                shapeLayer.fillColor = tintColorRef;
            }
        }
    }
}

#pragma mark -
#pragma mark Layout

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _animationLayer.frame = self.bounds;
    
    BOOL animating = _animating;
    
    if (animating)
        [self stopAnimating];
    
    [self setupAnimation];
    
    if (animating)
        [self startAnimating];
}

- (CGSize)intrinsicContentSize {
    return CGSizeMake(_size, _size);
}


@end
